import pymysql

from conn_mysql import ConnectMysql

import re

import datetime

class ApiVerify(ConnectMysql):

    def is_phone(self, phone):
        phone_pat = re.compile('^(13\d|14[5|7]|15\d|166|17[3|6|7]|18\d)\d{8}$')
        res = re.search(phone_pat, phone)
        if not res:
            return False
        return True

    def openid_notify_code(self,openid,verify_code):

        sql = 'insert into phone_notify_code(openid, phone_notify_code)values(%s, %s)'

        result_info = self.insert_info(sql, (openid, verify_code))

        return result_info

    def send_verify_code(self, phone, verify_code):

        pass

        return '发送成功'

    def verify_code(self, methods, openid, phone, verify_code):
        '''验证码发送'''

        if not methods == 'GET':
    
            return {
                'status': -1,
                'errorCode': 4,
                'errorMsg':f'请检查request.method:{methods}请重新请求',
                'resultBody':None
            }

        if not all((openid, phone, verify_code)):

            return {
                'status': -1,
                'errorCode': 1,
                'errorMsg':f'请检查参数是否为空,openid:{openid},phone{phone},verify_code{verify_code}请重新输入',
                'resultBody':None
            }

        phone_state = self.is_phone(phone)

        if not phone_state:

            return {
                    'status': -1,
                    'errorCode': 2,
                    'errorMsg':f'请检查参数是否合格,phone:{phone}请重新输入',
                    'resultBody':None
                }

        openid_notify_code = self.openid_notify_code(openid, verify_code)

        if not openid_notify_code:

            return {
                    'status': -1,
                    'errorCode': 5,
                    'errorMsg':'mysqlerror',
                    'resultBody':None
                }

        else:

            if self.send_verify_code(phone, verify_code):

                return {
                        'status': 1,
                        'errorCode': None,
                        'errorMsg':None,
                        'resultBody':'发送成功'
                    }

            else:

                return {
                    'status': -1,
                    'errorCode': 3,
                    'errorMsg':'发送失败',
                    'resultBody':None
                }





        

    